﻿package com.nicolasprof.profgallery.viewer {
	import gs.TweenLite;
	import gs.easing.Sine;

	import com.hexagonstar.util.debug.Debug;
	import com.nicolasprof.profgallery.Engine;
	import com.nicolasprof.profgallery.assets.ViewerBackground;
	import com.nicolasprof.profgallery.preloader.*;

	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.net.*;

	public class ImageHolder extends Sprite {

		private var _width:Number;
		private var _height:Number;
		private var _preloader : Preloader;
		private var _layers : Sprite;
		private var _engine : Engine;

		
		public function ImageHolder($viewWidth:Number, $viewHeight:Number, $engine : Engine) {
			_width=$viewWidth;
			_height=$viewHeight;
			_engine = $engine;
			_layers = new Sprite();
			
			this.addChild(_layers);

			drawViewer();
		}
		private function addLayer($realBmd : BitmapData) : void {
			
			if(_layers.numChildren > 1){
				var removed : Layer = _layers.removeChildAt(0) as Layer;
				removed.clear();		
			}
			if(_layers.numChildren > 0){
				this.adjustBottomLayer(_layers.getChildAt(0) as Layer);
			}
			
			var layer : Layer = new Layer();
			_layers.addChild(layer);
			layer.assignBitmapData($realBmd);
		}		
		private function adjustBottomLayer($layer : Layer) : void {
			var config: XML = Engine.CONFIG_XML;
			var alpha: Number = config.viewer.@firstLayerAlpha;
			if(alpha < 1){
				TweenLite.to($layer, 0.5, {alpha: alpha, ease: Sine.easeInOut});
			}
		}
		private function drawViewer():void {
			var config : XML = Engine.CONFIG_XML;
			var bg : ViewerBackground = new ViewerBackground();
			
			this.addChildAt(bg, 0);
			
			bg.width = _width;
			bg.height = _height;
			
			if(String(config.viewer.@activateURL).toLowerCase() == "true"){
				this.addEventListener(MouseEvent.CLICK, onClickViewer);
				this.buttonMode = true;
				this.tabEnabled = false;
			}	
			
			//PRELOADER
			switch (String(config.preloader.@type).toLowerCase()) {
				case "circle":
					_preloader = new CircleLoader();
					break;
				case "text":
					_preloader = new TextPreloader();
					break;
				default :
					_preloader = new Preloader();
					break;
			}
			_preloader.initiate();
			_preloader.x = _width>>1;
			_preloader.y = _height>>1;
			addChild(_preloader);	
		}
		
		public function loadPhoto($url:String):void {

			_preloader.toggle(true);
			_preloader.progress = 0;
			
			var loader : Loader = new Loader();
			var request : URLRequest = new URLRequest($url);
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading, false, 0, true);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
			loader.load(request);
		}

		/**
		 * EVENTS
		 */ 
		private function onComplete(e:Event):void {
			var bitmap : Bitmap = e.currentTarget.content;
			var ratio : Number = Math.min(_width / bitmap.width, _height / bitmap.height);
			ratio = Math.min(ratio,1); //Comment this line to enlarge small images to size of Viewer
			bitmap.smoothing = true;
			var data : BitmapData = new BitmapData(_width, _height, true, 0xFFFFFF);
			var matrix : Matrix = new Matrix(ratio,0,0, ratio);
			matrix.tx = (_width - bitmap.width * ratio) >> 1;
			matrix.ty = (_height - bitmap.height * ratio) >> 1;
			data.draw(bitmap, matrix);
			
			_preloader.toggle(false);

			dispatchEvent(e);
			
			addLayer(data);
		}
		private function onLoading(e:ProgressEvent):void {
			var ratio: Number = e.bytesLoaded / e.bytesTotal;
			_preloader.progress = ratio;
		}
		private function onClickViewer(e : MouseEvent) : void {
			Debug.trace("ImageHolder : Redirect!", 0);
			var url: String = _engine.getCurrentURL();
			var config: XML = Engine.CONFIG_XML;
			if(url == "") return;

			var request : URLRequest = new URLRequest(url);
			navigateToURL(request, String(config.viewer.@URLType));

		}
	}
}